from operator import itemgetter

number_hunted = 0
number_activity = 0 #number of times that could've hunted or hunted

def hunt_choices(round_number, current_food, current_reputation, m,  player_reputations):

    global number_hunted
    global number_activity

    length = len(player_reputations) # number of adversaries

    if round_number == 1:
        number_hunted += length
        number_activity += length
        hunt_decisions = ['h' for i in player_reputations]
        return hunt_decisions


    if length == 1: #the best choice for a 1x1 is to always slack
        number_activity += length
        hunt_decisions = ['s' for i in player_reputations]
        return hunt_decisions

    # sort vector for getting the median and trying to stay near it
    ordem = []
    for i in range(0, length):
        ordem.append(i)

    ordem = zip(player_reputations, ordem)
    ordem = sorted(ordem, key=itemgetter(0), reverse = True)

    median = ordem[int((length/2))][0]


    # compute the expected number of hunts of adversaries
    expected = 0
    for i in range(0, length):
        expected += player_reputations[i] * length


    duty = int(m/(length + 1)) # number of times everyone should hunt so we reach m

    lack = m - int(expected) # what i should contritube to get the threshold

    hunt_for_bonus = float(lack)/length # percentage of hunt i should do
                                # to get the bonus


    # new reputation if only hunted or only slacked
    nr_hunt_only = float((number_hunted + length))/(number_activity + length)
    nr_slack_only = float(number_hunted)/(number_activity + length)

    # choose to always hunt to keep reputation
    if (nr_hunt_only <= median):
        number_hunted += length
        number_activity += length
        hunt_decisions = ['h' for i in player_reputations]
        return hunt_decisions
    # choose to always slack if it doesnt affect reputation
    # but considering hunting for the bonus food
    elif (nr_slack_only >= median):
        number_activity += length
        hunt_decisions = ['s' for i in player_reputations]

        if (expected < m and lack <= duty and hunt_for_bonus <= current_reputation):
            number_hunted += lack
            for i in range(0, lack):
                hunt_decisions[ordem[i][1]] = 'h'

        return hunt_decisions
    # mix between slack and hunt to keep reputation above median
    # and considering the bonus food
    else:
        x = 0
        while (x < median * (number_activity + length) - number_hunted):
            x += 1

        hunt_decisions = ['s' for i in player_reputations]

        if (expected < m and lack <= duty and hunt_for_bonus <= current_reputation):
            if (x >= lack):
                number_hunted += x
                for i in range(0, x):
                    hunt_decisions[ordem[i][1]] = 'h'
            else:
                number_hunted += lack
                for i in range(0, lack):
                    hunt_decisions[ordem[i][1]] = 'h'
        else:
            number_hunted += x
            for i in range(0, x):
                hunt_decisions[ordem[i][1]] = 'h'

        number_activity += length

    return hunt_decisions



def hunt_outcomes(food_earnings):

    pass

def round_end(award, m, number_hunters):

    pass
